package org.sloth.core.load.proxy
{
	import org.sloth.core.load.LoadType;

	/**
	 * 加载代理工厂
	 */	
	public class LoadProxyFactory
	{
		private const _typeProxyMap:Object = {};
		private const _typeClassMap:Object = {};
		
		public function LoadProxyFactory():void
		{
			addType(LoadType.BINARY, LoadBinary);
			addType(LoadType.DISPLAYABLE, LoadDisplayable);
			addType(LoadType.MODULE, LoadModule);
			addType(LoadType.SOUND, LoadSound);
			addType(LoadType.TEXT, LoadText);
		}
		
		public function getProxy(type:String):ILoadProxy
		{
			if (!(type in _typeClassMap)) type = LoadType.BINARY;
			var pool:Array;
			if (type in _typeProxyMap)
				pool = _typeProxyMap[type];
			else
			{
				pool = [];
				_typeProxyMap[type] = pool;
			}
			
			var proxy:ILoadProxy;
			for each (proxy in pool)
			{
				if (!proxy.isLoading) return proxy;
			}
			proxy = new _typeClassMap[type]();
			pool.push(proxy);
			return proxy;
		}
		
		public function addType(type:String, cls:Class):void
		{
			_typeClassMap[type] = cls;
		}
	}
}